//
//  TKIMPopView.m
//  TKIM
//
//  Created by macpro on 15/12/2.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMPopView.h"
#import "TKIMMenuBar.h"
@interface TKIMPopView()
{
    NSArray * _subMenus;
}
/**子菜单的view(容器)*/
@property (nonatomic,weak)UIView * subMenuView;

@end

@implementation TKIMPopView

#pragma mark - 懒加载
-(UIView *)subMenuView
{
    if (!_subMenuView) {
        UIView * view = [[UIView alloc] init];
        _subMenuView = view;
        
//        UIImage * image = TKIMGetImage(@"im_bg_subMenu");
//        _subMenuView.layer.contents = (id)[self resizableImageWithImage:image].CGImage;
//        _subMenuView.layer.contents = (id)[self resizableImageWithImage:[UIImage imageNamed:@"im_bg_subMenu"]].CGImage;
        _subMenuView.layer.borderWidth = kSepLineWidth;
        _subMenuView.layer.cornerRadius = 5;
        _subMenuView.layer.masksToBounds = YES;
        _subMenuView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _subMenuView.backgroundColor = [TKIMColorHelper stringToColor:@"#EFEFF4"];
        [self addSubview:self.subMenuView];
    }
    return _subMenuView;
}

#pragma mark - 传值
-(void)showSubMenuViewWithSelectedButtonFrame:(CGRect)frame andSubMenus:(NSArray *)subMenus
{
    
    //弹出的小视图的高度
    CGFloat subListH = kSubButtonHeight * subMenus.count;
    CGFloat popW = frame.size.width-kSepLineWidth-7.5;
    CGFloat popH = subListH;
    CGFloat popX = frame.origin.x+4;
    CGFloat popY = self.height - popH - 5;//子菜单离底部菜单栏的间距设置为5
    self.subMenuView.frame = CGRectMake(popX, popY, popW, popH);
    
//    UIImage * image = TKIMGetImage(@"im_bg_subMenu");
//    self.subMenuView.layer.contents = (id)[self resizableImageWithImage:image].CGImage;

    
    CGRect newFrame = self.frame;
    newFrame.origin.y = subListH - frame.size.height;
    self.frame = newFrame;
    newFrame.origin.y = - frame.size.height;

    [UIView animateWithDuration:animationDuration animations:^{
        self.frame = newFrame;
    }];
    [self setSubMenus:subMenus];
}


-(void)setSubMenus:(NSArray *)subMenus
{
    _subMenus = subMenus;
    if (_subMenus.count == 0)  return;
    
    for (UIButton * subBtn in self.subMenuView.subviews) {
        [subBtn removeFromSuperview];
    }

    //按钮高度
    CGFloat buttonHeight = self.subMenuView.frame.size.height / _subMenus.count;
    for (NSInteger i = 0; i < _subMenus.count; i++)
    {
        TKIMMenuBarItem * item = _subMenus[i];
        //1.按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, buttonHeight * i, self.subMenuView.frame.size.width, buttonHeight - kSepLineWidth);
        [button setTitle:item.menu_name forState:UIControlStateNormal];
        button.titleLabel.font = kSubButtonFont;
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.tag = i+1;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.subMenuView addSubview:button];
        
        //2.线
        CGFloat lineW = button.frame.size.width*0.8;
        CGFloat lineX = (button.frame.size.width -lineW)*0.5;
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(lineX, CGRectGetMaxY(button.frame),lineW, kSepLineWidth)];
        line.backgroundColor = [UIColor lightGrayColor];
        [self.subMenuView addSubview:line];
    }
}

-(void)hideSubMenuWithAnimationHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.origin.y = height;
    [UIView animateWithDuration:animationDuration animations:^{
        self.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - 监听事件
- (void)buttonClick:(UIButton *)button
{
    //1.隐藏子菜单
    [self hideSubMenuWithAnimationHeight:self.subMenuView.height];
    self.menuButton.selected = NO;
    //2.跳转页面或发消息
    TKIMMenuBarItem * item = _subMenus[button.tag - 1];
    if ([item.type isEqualToString:@"link"]) {
        [self routerEventWithName:TKIMSubMenuClickEvent userInfo:item];
    }else if([item.type isEqualToString:@"click"]){
         [self routerEventWithName:TKIMMenuBarOnclickEvent userInfo:item.key];
    }
}

-(UIImage *)resizableImageWithImage:(UIImage *)orignImage
{
//    NSLog(@">>>%@",NSStringFromCGSize(orignImage.size));
//    return [orignImage stretchableImageWithLeftCapWidth:orignImage.size.width*0.5 topCapHeight:orignImage.size.height*0.5];
    return [orignImage resizableImageWithCapInsets:UIEdgeInsetsMake(orignImage.size.height*0.5, orignImage.size.width*0.5, orignImage.size.height*0.5, orignImage.size.width*0.5) resizingMode:UIImageResizingModeTile];
}
@end
